﻿using System;
using System.Configuration;
using Owin;
using Sustainalytics.DataLayer;
using Sustainalytics.DynamicsCRMGateway;
using Sustainalytics.Claims;
using Sustainalytics.Accounts.BusinessLogic;
using System.Collections.Generic;
using Sustainalytics.Controversies.BusinessLogic;
using Sustainalytics.Entities;

namespace Sustainalytics.Admin.CRMSynchronization
{
    public class Startup
    {
        public void Configuration(IAppBuilder builder)
        {
            var syncInterval = TimeSpan.FromSeconds(double.Parse(ConfigurationManager.AppSettings["SecondsBetweenSyncs"]));
            string clientlatformUrl = ConfigurationManager.AppSettings["clientPlatformUrl"];
            
            if (string.IsNullOrEmpty(clientlatformUrl))
                throw new Exception("clientplatformUrl key is empty or missing.");
            
            var connString = ConfigurationManager.ConnectionStrings["SustainalyticsCRM"].ConnectionString;
            var crmGateway = DynamicsCrmGateway.GetInstance(connString);
            var connStringClientPlatform = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
            var accountsRepository = AccountsRepository.GetAccountsRepository(connStringClientPlatform);
            var companyRepository = CompanyScreeningRepository.GetCompanyScreeningRepository(connStringClientPlatform);
          
            var ipDataCheck = new IpDataCheck(PlainBlFactory.GetInstance<AccountIpAuthentication>(), PlainBlFactory.GetInstance<User>());
            var userIpLoginManager=new UserIpLoginManager(PlainBlFactory.GetInstance<User>(connStringClientPlatform), 
                PlainBlFactory.GetInstance<ClientUser>(connStringClientPlatform));
            var accountsSync = AccountsSynchronization.GetInstance(crmGateway, accountsRepository, companyRepository, 
                new OperationClaimsRetriever(), new AccountWithIpManager(PlainBlFactory.GetInstance<AccountIpAuthentication>(), userIpLoginManager, accountsRepository,ipDataCheck));
            var serviceBusParams = new Dictionary<string, string>
                {
                        { "ConnectionString", ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"] },
                        { "RegionEndpoint", ConfigurationManager.AppSettings["Microsoft.ServiceBus.RegionEndpoint"] },
                        { "SessionId", ConfigurationManager.AppSettings["Microsoft.ServiceBus.SessionId"] },
                        { "TopicPath", ConfigurationManager.AppSettings["Microsoft.ServiceBus.TopicPath"] },
                };
            var authOperations = new AuthOperations.AuthOperations(accountsRepository, clientlatformUrl, serviceBusParams);
            var usersSync = UsersSynchronization.GetInstance(crmGateway, accountsRepository, authOperations);

            bool dump = false;
            var dumpToFile = ConfigurationManager.AppSettings["DumpToFile"];
            if (dumpToFile != null)
                bool.TryParse(dumpToFile, out dump);

            var claimsManager = new ClaimsManager(crmGateway, accountsRepository, new CRMEntityDumper(dump));
            var claimsRetriever = new OperationClaimsRetriever();
            var claimsSync = new ClaimsSynchronization(crmGateway, claimsManager, claimsRetriever, accountsRepository, 
                ConfigurationManager.AppSettings["PermissionsToRemainUntouchedInRedis"], connStringClientPlatform);

            Admin.CRMSynchronization.Synchronization.GetInstance(accountsSync, usersSync, claimsSync, syncInterval).Start();
        }
    }
}
